package com.example.g5project.repository.dao;

import com.example.g5project.entity.frontEndObject.NewChat;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName ConsultantDao.java
 * @Description TODO
 * @createTime 2022年03月25日 15:08:00
 */
@Repository
@Transactional
@SuppressWarnings("all")
public interface NewChatDao extends JpaRepository<NewChat, Integer>, JpaSpecificationExecutor {
    @Query(value = "select * from chat,customer where chat.caller_id = customer.id and customer.name = ?1 and chat.start_time = ?2", nativeQuery = true)
    ArrayList<NewChat> getChatMsgByNameAndDate(String name, String date);

    @Modifying
    @Query(value = "update chat set  comment_counselor = ?2 where id = ?1", nativeQuery = true)
    void updateNewChatComment(Integer id, String content);

    @Query(value = "select * from chat,consultant where chat.counselor_id = consultant.consultant_id and  consultant.name = ?1 order by chat.id desc ", nativeQuery = true)
    ArrayList<NewChat> getAllChatMsgByConsultantName(String name);

    @Query(value = "select end_time from chat where id = ?1", nativeQuery = true)
    String getEndTimeById(Integer id);

    @Query(value = "select * from chat", nativeQuery = true)
    ArrayList<NewChat> getAllMsg();

    @Query(value = "select   *   from   chat   order   by   id   desc   limit   1;", nativeQuery = true)
    NewChat getRecentMsg();

    @Query(value = "select   end_time   from   chat   order   by   id   desc   limit   1;", nativeQuery = true)
    String getRecentMsgEndTime();

    @Modifying
    @Query(value = "update chat,consultant set hold_time = ?2 where chat.counselor_id = consultant.consultant_id  and consultant.name = ?1 ", nativeQuery = true)
    void updateHoldTimeByName(String name, String hold_time);

    @Modifying
    @Query(value = "update chat,consultant set hold_time = ?2 where chat.counselor_id = consultant.consultant_id  and id = ?1 ", nativeQuery = true)
    void updateHoldTimeByChatID(Integer id, String hold_time);
}

